package com.ja.openchat.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.ja.openchat.BR;
import com.ja.openchat.R;
import com.ja.openchat.databinding.FragmentSetNickNameBinding;
import com.ja.openchat.viewmodels.SetNickNameViewModel;
import com.zy.openchat.util.MediaUtils;
import com.zy.openchat.util.glide.GlideApp;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;

public class SetNickNameFragment extends BaseFragment<FragmentSetNickNameBinding, SetNickNameViewModel> {

    Bundle arguments;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_set_nick_name;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
        arguments = getArguments();
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.setCurUserInfo(arguments);
        binding.setAvatarView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MediaUtils.choosePhotoFg(SetNickNameFragment.this, true);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<String> photos = MediaUtils.getResultAlbumData(getActivity(), requestCode, resultCode, data);
        if (photos != null && photos.size() > 0) {
            setUserHeader(photos.get(0));
        }
    }

    private void setUserHeader(String localPath) {
        if(TextUtils.isEmpty(localPath)) {
            return;
        }
        binding.llAvatarTop.setVisibility(View.INVISIBLE);
        viewModel.localHeadImage = localPath;
        GlideApp.with(binding.setAvatar).load(localPath).into(binding.setAvatar);
    }
}